package com.ruding.disruptor.utils.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 数据发送器
 * @author 520475
 *
 */
public class DataSender {
    
    private static final Logger LOG = LoggerFactory.getLogger(DataSender.class);
    
    /**
     * 发送
     * @param json
     * @param config
     * @return
     */
    public static boolean send(String url,JSONObject json) {
        LOG.info(json.toJSONString());
        try {
            HttpResult result = HttpClient.build(null).post(url, json);
            if(result.getStatusCode() == 200) {
                JSONObject resp = JSON.parseObject(new String(result.getBody()));
                Boolean success = false;
                if(resp != null) {
                    success = resp.getBoolean("success") == null ? false : resp.getBoolean("success");
                    if(!success) {
                        LOG.error("<send> response={}", resp.toString());
                    }
                }else {
                    LOG.error("<send> response=null");
                }
                return success;
            }else {
                return false;
            }
        } catch (Exception e) {
            LOG.error("<send> failed, error={}, ", e.getMessage(), e);
            return false;
        }
    }
    

}
